<template>
  <div id="app">
    <!--<router-view></router-view>-->
    <transition name="slide-fade">
      <router-view></router-view>
    </transition>
    <foot v-if="this.$store.state.ethWallet.showNav" :index="$route.params.index?$route.params.index:1"></foot>
  </div>
</template>

<script>
  import Foot from "./page/eth/Foot"
  import store from "./store"
  import {mapGetters, mapActions} from 'vuex'
  import accountUtils from "./service/eth/accountUtils.js";
  import axios from 'axios'
  export default {
    name: 'App',
    computed: {
      ...mapGetters(['coinsList','exchangeMap','currentAccount','switchCoins',])
    },
    methods: {
      ...mapActions(['setCoinsList','setExchangeMap','setSwitchCoins',]),
      getCoinList() {
        let _this=this;
        if(!this.coinsList){
          let list=require("./service/eth/config").mainCoinList
          for(let i=0;i<coinsList.length;i++){
            let {abi,address}=require("./service/coin/"+coinsList[i].name);
            coinsList[i].abi=abi;
          }
          console.log(coinsList)
          _this.setCoinsList(coinsList)
        }
        //
        // if(!this.supportCoinArray){
        //   let supportCoinArray=require("./service/base/supportTokens").supportCoinArray
        //   console.log(supportCoinArray)
        //   _this.setSupportCoinArray(supportCoinArray)
        // }
        //
        // if(!this.mainCoin){
        //   let mainCoin=require("./service/base/supportTokens").mainCoin
        //   console.log(mainCoin)
        //   _this.setMainCoin(mainCoin)
        // }


        // axios.get(_this.global.apiPath+'/coins/getCoins').then(resp => {
        //   console.log(resp)
        //   if (resp.status == 200 && resp.data) {
        //     if(!_this.coinsList||_this.coinsList.length!=resp.data.data.length){
        //       console.log('刷新')
        //       let dataList=resp.data.data
        //
        //       for(let i=0;i<dataList.length;i++){
        //           dataList[i].abi=JSON.parse(dataList[i].abi)
        //       }
        //
        //       _this.setCoinsList(dataList)
        //       console.log(dataList)
        //    }
        //   }
        // })
      },
      getExchangeMaps() {
        let _this=this;
        axios.get(_this.global.apiPath+'/exchange/getExchangeMaps').then(resp => {
          if (resp.status == 200 && resp.data) {
            if(!_this.coinsList||_this.coinsList.length!=resp.data.data.length){
              let dataList=resp.data.data
              console.log(dataList)
              for(let i=0;i<dataList.length;i++){
                dataList[i].abi=JSON.parse(dataList[i].abi)

                for(let j=0;j<dataList[i].supportCoinArray.length;j++){
                  console.log(dataList[i].supportCoinArray[j])
                  dataList[i].supportCoinArray[j].abi=JSON.parse(dataList[i].supportCoinArray[j].abi)
                }
              }

              _this.setExchangeMap(dataList)
              console.log(_this.exchangeMap)
            }
          }
        })
      },
      // async  getRate() {
      //   let _this=this
      //   await axios.get('https://dfpchain.com:7001/rate/getRate').then(resp => {
      //     _this.global.setRate(parseFloat(resp.data.data.rate))
      //   });
      // },
      // getCoinPrice(){
      //   let _this=this
      //   axios.get('https://dfpchain.com:7001/ticker/getLastTicker').then(resp => {
      //     if (resp.status == 200 && resp.data) {
      //       for (let j = 0; j < resp.data.data.length; j++) {
      //         if (resp.data.data[j].symbol == 'ETH') {
      //           _this.currentAccount.ethPrice = resp.data.data[j].priceUsd;
      //           continue;
      //         }
      //         for (let i = 0; i < _this.coinsList.length; i++) {
      //           if (_this.coinsList[i].name == resp.data.data[j].symbol) {
      //             _this.coinsList[i].price = resp.data.data[j].priceUsd;
      //             break;
      //           }
      //         }
      //       }
      //     }
      //   });
      // },
      // async getEthBalance() {
      //   let _this = this;
      //   let ethToMoney = 0;
      //   let coinMoney = 0;
      //   let asyncArr=[]
      //   if(!_this.currentAccount.coins){
      //     _this.currentAccount.coins=[]
      //   }
      //
      //
      //   if(_this.global.rate==0){
      //     await this.getRate()
      //   }
      //
      //   await this.getCoinPrice()
      //
      //   for (let i = 0; i < _this.switchCoins.length; i++) {
      //     let tokenCont=new _this.global.web3.eth.Contract(JSON.parse(_this.switchCoins[i].abi),_this.switchCoins[i].contractaddress);
      //     asyncArr.push(tokenCont.methods.balanceOf(_this.currentAccount.address).call())
      //   }
      //   asyncArr.push(_this.global.web3.eth.getBalance(_this.currentAccount.address))
      //
      //   Promise.all(asyncArr).then((data)=>{
      //     console.log(data)
      //     for(let i=0;i<data.length-1;i++){
      //       _this.switchCoins[i].balance = data[i] > 0?data[i]:0;
      //       coinMoney += parseFloat(((data[i] / Math.pow(10, _this.switchCoins[i].decimals)) * parseFloat(_this.switchCoins[i].price) * _this.global.rate).toFixed(4));
      //     }
      //     let ethBal=data[data.length-1]
      //
      //     ethToMoney =parseFloat(ethBal / Math.pow(10, 18) * _this.currentAccount.ethPrice * _this.global.rate).toFixed(4);
      //     _this.currentAccount.balance = (parseFloat(ethToMoney) + parseFloat(coinMoney)).toFixed(4)
      //
      //     _this.currentAccount.ethBalance = ethBal
      //     console.log((parseFloat(ethToMoney) + parseFloat(coinMoney)).toFixed(4))
      //     if (ethBal > 0) {
      //       _this.currentAccount.eth = _this.global.web3.utils.fromWei(ethBal + '', 'ether')
      //     } else {
      //       _this.currentAccount.eth = 0
      //     }
      //
      //    // Indicator.close()
      //   }).catch((error)=>{
      //     console.log(error)
      //   })
      // }
    },
    watch: {
      $route(to, from) {
        paths = to.path;

        if(to.path=='/'){
          mainIndex=1;
        }else if(to.path=='/change'){
          mainIndex=2;
        }else if(to.path=='/market'){
          mainIndex=3;
        }else if(to.path=='/my'){
          mainIndex=4;
        }
        // console.log(to);
        // console.log(from)

      }
    },
    store,
    components: {
      Foot,
    },
    created() {
      this.getCoinList()
    //  this.getExchangeMaps()
    //  this.getEthBalance()
    }
  }
</script>

<style scoped>
  #app {
    /*font-family: 'Avenir', Helvetica, Arial, sans-serif;*/
    /*-webkit-font-smoothing: antialiased;*/
    /*-moz-osx-font-smoothing: grayscale;*/
    /*text-align: center;*/
    /*color: #2c3e50;*/
    /*width:100%;*/
  }

  .slide-fade {
    position: absolute;
    left: 0;
    right: 0;
  }

  .slide-fade-enter-active {
    transition: all .4s ease;
  }

  .slide-fade-leave-active {

    transition: all .1s cubic-bezier(2.0, 0.5, 0.8, 1.0);
  }

  .slide-fade-enter, .slide-fade-leave-to {
    left: 0;
    right: 0;
    transform: translateY(50px);
    opacity: 0;
  }

</style>
